/**
 * org.assess.oa.system.service.impl.TbUserServiceImpl
 */
package org.assess.oa.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.assess.core.common.SecurityUtils;
import org.assess.core.service.BaseServiceImpl;
import org.assess.oa.system.bean.TbUser;
import org.assess.oa.system.dao.TbUserDAO;
import org.assess.oa.system.service.TbUserService;

/**
 * @description 用户管理的Service层实现类
 * @author XiaLongxiang
 * @createtime 2012-5-17 下午12:14:56
 */
public class TbUserServiceImpl extends BaseServiceImpl implements TbUserService {
    /** 操作用户表的DAO层对象 */
    private TbUserDAO tbUserDAO;
    
    /** 日志对象 */
    private static final Logger logger = Logger.getLogger(TbUserServiceImpl.class);
    
    public TbUser queryTbUserByCode(TbUser user) {
        TbUser result = null;
        try {
            result = this.tbUserDAO.queryTbUserByCode(user.getUser_code());
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String queryTbUserAsString(int page, int rows, TbUser user) {
        String result = "";
        try {
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("valid_flag", user.getValid_flag());
            parameters.put("user_code", user.getUser_code());
            parameters.put("department_name", user.getDepartment_name());
            int totalrecords = this.tbUserDAO.queryTbUserAsInt(parameters);
            List<TbUser> records = this.tbUserDAO.queryTbUserAsList((page - 1) * rows, rows, parameters);
            result = super.toListPageString(page, rows, totalrecords, "getUser_code", records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        
        return result;
    }
    
    public String checkUser(TbUser user) {
        String result = "";
        try {
            TbUser record = this.tbUserDAO.queryTbUserByCode(user.getUser_code());
            result = super.toCheckExistsString(record != null && StringUtils.isNotEmpty(record.getUser_code()));
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String updateValid(TbUser user) {
        String result = "";
        try {
            String[] user_codes = user.getUser_code().split(",");
            user.setModify_time(new Date());
            user.setModify_user(super.getLoginUserCode());
            for (String code : user_codes) {
                user.setUser_code(code);
                this.tbUserDAO.updateTbUserValid(user);
            }
            
            result = super.toResultString(true);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public boolean saveUser(TbUser user, boolean edit) {
        boolean result = false;
        try {
            if (StringUtils.isBlank(user.getFreeze_flag())) {
                user.setFreeze_flag("0");
            }
            if (StringUtils.isNotBlank(user.getPassword())) {
                byte[] bps = SecurityUtils.encryptMD5(user.getPassword().getBytes());
                user.setPassword(org.assess.core.common.StringUtils.parse16(bps, false));
            }
            
            TbUser record = this.tbUserDAO.queryTbUserByCode(user.getUser_code());
            if (edit) {
                if (record != null) {
                    if (StringUtils.isBlank(user.getPassword())) {
                        user.setPassword(record.getPassword());
                    }
                    
                    user.setModify_time(new Date());
                    user.setModify_user(super.getLoginUserCode());
                    result = this.tbUserDAO.update(user);
                }
            }
            else if (record == null) {
                user.setValid_flag("1");
                user.setCreate_time(new Date());
                user.setCreate_user(super.getLoginUserCode());
                
                result = this.tbUserDAO.insert(user);
            }
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    /**
     * 设置操作用户表的DAO层对象
     * @param tbUserDAO
     */
    public void setTbUserDAO(TbUserDAO tbUserDAO) {
        this.tbUserDAO = tbUserDAO;
    }
}
